const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    info: null,
    shop_info: null,
    need: {
      days: 0,
      hours: 0,
      minutes: 0,
      second: 0
    },
    options:null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    wx.showLoading({
      title: '加载中…',
    })
    let that = this;
    //ver-jionEndTime
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/verJionEndTime/id/' + options.id, function (res) {
      that.setData({
        verJoinEndTime: res.data
      })
    })
    if(options.type){
      var url = 'https://www.58zltc.com/api.php/Goods/item/type/' + options.type + '/id/' + options.id + '/openid/' + options.openid + '/kan_openid/' + app.globalData.openid;
    }else{
      var url = 'https://www.58zltc.com/api.php/Goods/item/openid/' + app.globalData.openid+'/id/' + options.id
    }
    that.setData({
      options: options
      })
    console.log(url);
    app.ihttpGet(url, function(res){
      
      var WxParse = require('../../wxParse/wxParse.js');
      var article = res.data.content;
      WxParse.wxParse('article', 'html', article, that, 5);

      that.setData({
        info: res.data,
        kan_openid: app.globalData.openid,
        openid: options.openid
      })
      if (res.data.is_qiang == 1 && res.data.qiang_time != 2) {
        that.setCountdown();
      }
      that.setShopInfo(res.data.shop_id);
      wx.setNavigationBarTitle({
        title: res.data.title,
      })
    })
  },
  setCountdown: function(){
    let that = this;
    var d_minutes, d_hours, d_days, d;
    var timeNow = parseInt(new Date().getTime() / 1000);
    var data = this.data.info;
    if(data.qiang_time == 0){
      var pTime = data.start_date+' '+data.start_time;
    } else if (data.qiang_time == 1){
      var pTime = data.end_date + ' ' + data.end_time;
    }
    pTime = pTime.replace(/-/g, '/');
    var pTime_new = new Date(pTime).getTime() / 1000;
    d = Math.abs(timeNow - pTime_new);

    var need = {};
    need.days = parseInt(d / 86400);
    d = d % 86400;
    need.hours = parseInt(d / 3600);
    d = d % 3600;
    need.minutes = parseInt(d / 60);
    need.second = d % 60;
    this.setData({
      need:need
    })
    setTimeout(function(){
      that.setCountdown();
    }, 1000);
  },
  setTab: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.id
    })
  },
  setShopInfo: function (shop_id){
    let that = this;
    app.ihttpGet('https://www.58zltc.com/api.php/Shop/item/shop_id/' + shop_id, function(res){
      that.setData({
        shop_info: res.data
      })
    })
  },
  kan: function(e){
    var that1 = this;
    app.ihttpGet('https://www.58zltc.com/api.php/Goods/addKanLog/type/kan/id/' + this.data.info.id + '/openid/' + this.data.openid + '/kan_openid/' + this.data.kan_openid, function (res) {
      app.iModel(res.data.err_msg);
      var options = that1.data.options;
      console.log(options);



      wx.showLoading({
        title: '加载中…',
      })
      let that = that1;
      if (options.type) {
        var url = 'https://www.58zltc.com/api.php/Goods/item/type/' + options.type + '/id/' + options.id + '/openid/' + options.openid + '/kan_openid/' + app.globalData.openid;
      } else {
        var url = 'https://www.58zltc.com/api.php/Goods/item/openid/' + app.globalData.openid + '/id/' + options.id
      }

      app.ihttpGet(url, function (res) {
        that.setData({
          info: res.data,
          kan_openid: app.globalData.openid,
          openid: options.openid
        })
        if (res.data.is_qiang == 1 && res.data.qiang_time != 2) {
          that.setCountdown();
        }
        that.setShopInfo(res.data.shop_id);
        wx.setNavigationBarTitle({
          title: res.data.title,
        })
      })





      })
      
  },
  pay: function(e){
    const order_type = e.currentTarget.dataset.type;
    var post_data = {};
    post_data.id = this.data.info.id;
    // var url = '/pages/shop/pintuan/share?id=' + this.data.info.tuan_first_id;
    // wx.navigateTo({
    //   url: url,
    // })
    post_data.openid = app.globalData.openid;
    if (order_type){
      post_data.type = order_type;
    }

    app.ihttpPost("https://www.58zltc.com/api.php/Goods/addGoodsOrder", post_data, function(res){
      if (res.data.err_code == 400) {
        app.iModel(res.data.err_msg);
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              if(order_type == 'pin'){
                var url = '/pages/shop/pintuan/share';

               // var url = '/pages/mine/order/order';
              }else{
                var url = '/pages/mine/order/order';
              }
              wx.navigateTo({
                url: url,
              })
            }
          }
        })
      }
    })
  },
  goShop: function () {
    wx.navigateTo({
      url: '../jiancai/detail?shop_id='+this.data.info.shop_id,
    })
  },
  goPintuan: function(e){
    wx.navigateTo({
      url: '/pages/shop/pintuan/share?id=' + e.currentTarget.dataset.id,
    })
  },
  bindCall: function () {
    var tel = this.data.shop_info.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var options = this.data.options;
    console.log(options);
    wx.showLoading({
      title: '加载中…',
    })
    let that = this;
    if (options.type) {
      var url = 'https://www.58zltc.com/api.php/Goods/item/type/' + options.type + '/id/' + options.id + '/openid/' + options.openid + '/kan_openid/' + app.globalData.openid;
    } else {
      var url = 'https://www.58zltc.com/api.php/Goods/item/openid/' + app.globalData.openid + '/id/' + options.id
    }

    app.ihttpGet(url, function (res) {
      that.setData({
        info: res.data,
        kan_openid: app.globalData.openid,
        openid: options.openid
      })
      if (res.data.is_qiang == 1 && res.data.qiang_time != 2) {
        that.setCountdown();
      }
      console.log(res)
      that.setShopInfo(res.data.shop_id);
      wx.setNavigationBarTitle({
        title: res.data.title,
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if(this.data.info.is_kan == 1){
      let that = this;
      return {
        title: '我正在参加' + this.data.info.title+'砍价活动！',
        path: '/pages/goods/detail?type=kan&openid='+app.globalData.openid+'&id=' + this.data.info.id,
        success: function (res) {
          app.ihttpGet("https://www.58zltc.com/api.php/Goods/addKanLog/openid/" + app.globalData.openid + "/id/" + that.data.info.id, function(res){

          })
          // 转发成功
          wx.showToast({
            title: '转发成功！',
          })
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
  }
})